<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>幸运抽奖转盘</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
        }

        body {
            min-height: 100vh;
            background: linear-gradient(135deg, #1a2a6c 0%, #b21f1f 50%, #fdbb2d 100%);
            background-attachment: fixed;
            padding: 20px;
            color: #333;
            display: flex;
            flex-direction: column;
            align-items: center;
        }

        .container {
            width: 100%;
            max-width: 1200px;
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 30px;
            margin: 20px auto;
        }

        .panel {
            background: rgba(255, 255, 255, 0.95);
            border-radius: 24px;
            box-shadow: 0 10px 40px rgba(0, 0, 0, 0.15);
            padding: 30px;
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }

        .panel:hover {
            transform: translateY(-5px);
            box-shadow: 0 15px 45px rgba(0, 0, 0, 0.2);
        }

        .control-panel {
            display: flex;
            flex-direction: column;
        }

        h1 {
            text-align: center;
            color: #fff;
            margin: 20px 0 30px;
            text-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
            font-size: clamp(1.8rem, 5vw, 2.8rem);
            width: 100%;
            background: rgba(255, 255, 255, 0.1);
            padding: 15px 20px;
            border-radius: 15px;
            backdrop-filter: blur(10px);
            border: 1px solid rgba(255, 255, 255, 0.2);
            max-width: 800px;
        }

        h2 {
            color: #2c3e50;
            margin-bottom: 25px;
            font-size: clamp(1.5rem, 3vw, 1.8rem);
            text-align: center;
            position: relative;
            padding-bottom: 12px;
        }

        h2::after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 50%;
            transform: translateX(-50%);
            width: 60px;
            height: 4px;
            background: linear-gradient(90deg, #ff6b6b, #ffda79);
            border-radius: 2px;
        }

        .input-group {
            margin-bottom: 25px;
        }

        label {
            display: block;
            margin-bottom: 10px;
            font-weight: 600;
            color: #2c3e50;
            font-size: 1.1rem;
            display: flex;
            align-items: center;
        }

        label i {
            margin-right: 8px;
            color: #e74c3c;
        }

        input[type="number"],
        input[type="text"] {
            width: 100%;
            padding: 14px 18px;
            border: 2px solid #e0e0e0;
            border-radius: 12px;
            font-size: 1.1rem;
            transition: all 0.3s ease;
            margin-bottom: 10px;
            background: #f9f9f9;
        }

        input[type="number"]:focus,
        input[type="text"]:focus {
            border-color: #3498db;
            outline: none;
            box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.2);
            background: #fff;
        }

        button {
            color: white;
            border: none;
            padding: 14px 25px;
            border-radius: 12px;
            cursor: pointer;
            font-size: 1.1rem;
            font-weight: 600;
            transition: all 0.3s ease;
            width: 100%;
            margin: 10px 0;
            letter-spacing: 1px;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
            position: relative;
            overflow: hidden;
        }

        button::before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
            transition: 0.5s;
        }

        button:hover::before {
            left: 100%;
        }

        button:hover {
            transform: translateY(-3px);
            box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
        }

        button:active {
            transform: translateY(1px);
        }

        .spin-btn {
            background: linear-gradient(135deg, #ff5e5e, #ff3838);
            font-size: 1.3rem;
            padding: 18px;
            margin-top: 20px;
            border-radius: 15px;
        }

        .add-btn {
            background: linear-gradient(135deg, #4cd964, #28a745);
            font-size: 1rem;
            padding: 12px;
            margin-top: 10px;
        }

        #update-btn {
            background: linear-gradient(135deg, #3498db, #2980b9);
        }

        .wheel-container {
            position: relative;
            width: 100%;
            max-width: 500px;
            margin: 0 auto;
            padding: 20px;
        }

        .wheel {
            position: relative;
            width: 100%;
            padding-bottom: 100%;
            border-radius: 50%;
            overflow: hidden;
            box-shadow: 0 0 0 15px #ffffff, 0 0 0 18px #f0f0f0, 0 15px 35px rgba(0, 0, 0, 0.3);
            transition: transform 4s cubic-bezier(0.17, 0.67, 0.21, 0.99);
        }

        .wheel::after {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: radial-gradient(circle at 50% 50%, rgba(255, 255, 255, 0) 65%, rgba(0, 0, 0, 0.1) 100%);
            pointer-events: none;
        }

        .wheel-inner {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            border-radius: 50%;
            display: flex;
            justify-content: center;
            align-items: center;
            background: conic-gradient(from 0deg,
                    #FF6B6B 0deg 36deg,
                    #FFD166 36deg 72deg,
                    #06D6A0 72deg 108deg,
                    #118AB2 108deg 144deg,
                    #073B4C 144deg 180deg,
                    #9381FF 180deg 216deg,
                    #F8B195 216deg 252deg,
                    #F67280 252deg 288deg,
                    #C06C84 288deg 324deg,
                    #6C5B7B 324deg 360deg);
        }

        .center-circle {
            position: absolute;
            width: 80px;
            height: 80px;
            background: white;
            border-radius: 50%;
            z-index: 10;
            box-shadow: 0 0 15px rgba(0, 0, 0, 0.2);
            display: flex;
            justify-content: center;
            align-items: center;
            font-weight: bold;
            color: #e74c3c;
            border: 4px solid #f0f0f0;
            font-size: 1.2rem;
            cursor: pointer;
            transition: transform 0.3s ease;
        }

        .center-circle:hover {
            transform: scale(1.05);
        }

        .pointer {
            position: absolute;
            top: -20px;
            left: 50%;
            transform: translateX(-50%) rotate(180deg);
            width: 45px;
            height: 65px;
            background: linear-gradient(180deg, #ff5e5e, #c0392b);
            clip-path: polygon(50% 0%, 0% 100%, 100% 100%);
            z-index: 20;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
            border-top: 3px solid rgba(255, 255, 255, 0.3);
        }

        .pointer::after {
            content: '';
            position: absolute;
            top: 100%;
            left: 50%;
            transform: translateX(-50%);
            width: 18px;
            height: 35px;
            background: #c0392b;
            border-radius: 0 0 8px 8px;
        }

        .pointer::before {
            content: '';
            position: absolute;
            top: 15px;
            left: 50%;
            transform: translateX(-50%);
            width: 10px;
            height: 10px;
            background: rgba(255, 255, 255, 0.4);
            border-radius: 50%;
        }

        .sector-label {
            position: absolute;
            top: 50%;
            left: 50%;
            width: 42%;
            text-align: center;
            font-size: clamp(0.85rem, 2vw, 1rem);
            font-weight: bold;
            color: white;
            text-shadow: 0 2px 4px rgba(0, 0, 0, 0.4);
            transform-origin: 0 0;
            transform: translate(-50%, -50%) rotate(var(--angle)) translateY(-160px) rotate(var(--reverse-angle));
            transition: all 0.3s ease;
            padding: 5px;
        }

        .result {
            text-align: center;
            padding: 25px;
            margin-top: 30px;
            background: #f8f9fa;
            border-radius: 18px;
            min-height: 120px;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            font-size: clamp(1.3rem, 3vw, 1.5rem);
            font-weight: bold;
            color: #2c3e50;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05), inset 0 0 10px rgba(0, 0, 0, 0.05);
            border: 1px solid rgba(0, 0, 0, 0.07);
        }

        .result i {
            font-size: 3rem;
            margin-bottom: 15px;
            color: #e74c3c;
            animation: pulse 2s infinite;
        }

        @keyframes pulse {
            0% {
                transform: scale(1);
            }

            50% {
                transform: scale(1.1);
            }

            100% {
                transform: scale(1);
            }
        }

        .instructions {
            background: #f8f9fa;
            padding: 25px;
            border-radius: 18px;
            margin-top: 25px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
            border: 1px solid rgba(0, 0, 0, 0.07);
        }

        .instructions h3 {
            color: #2c3e50;
            margin-bottom: 15px;
            text-align: center;
            font-size: 1.3rem;
        }

        .instructions ul {
            padding-left: 25px;
        }

        .instructions li {
            margin-bottom: 12px;
            line-height: 1.6;
            color: #34495e;
            position: relative;
            padding-left: 8px;
        }

        .instructions li::before {
            content: '•';
            color: #e74c3c;
            font-weight: bold;
            position: absolute;
            left: -15px;
        }

        .item-inputs {
            max-height: 300px;
            overflow-y: auto;
            margin-bottom: 20px;
            padding-right: 10px;
        }

        .item-inputs::-webkit-scrollbar {
            width: 8px;
        }

        .item-inputs::-webkit-scrollbar-track {
            background: #f1f1f1;
            border-radius: 10px;
        }

        .item-inputs::-webkit-scrollbar-thumb {
            background: #ddd;
            border-radius: 10px;
        }

        .item-inputs::-webkit-scrollbar-thumb:hover {
            background: #bbb;
        }

        .item-input {
            display: flex;
            align-items: center;
            margin-bottom: 12px;
            animation: fadeIn 0.3s ease;
        }

        @keyframes fadeIn {
            from {
                opacity: 0;
                transform: translateY(10px);
            }

            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .item-input input {
            flex: 1;
            margin-right: 10px;
        }

        .item-input button {
            width: auto;
            padding: 8px 12px;
            margin: 0;
            background: #e74c3c;
            font-size: 0.9rem;
            border-radius: 8px;
        }

        .spin-animation {
            animation: spin 4s cubic-bezier(0.17, 0.67, 0.21, 0.99) forwards;
        }

        @keyframes spin {
            0% {
                transform: rotate(0deg);
            }

            20% {
                transform: rotate(720deg);
            }

            100% {
                transform: rotate(var(--rotation));
            }
        }

        .update-hint {
            color: #e74c3c;
            font-size: 0.9rem;
            margin-top: 5px;
            text-align: center;
            opacity: 0;
            transition: opacity 0.3s, transform 0.3s;
            transform: translateY(5px);
        }

        .update-hint.show {
            opacity: 1;
            transform: translateY(0);
        }

        .confetti {
            position: fixed;
            width: 10px;
            height: 10px;
            background-color: #f00;
            animation: confetti-fall 5s ease-in-out forwards;
            pointer-events: none;
            z-index: 100;
        }

        @keyframes confetti-fall {
            0% {
                transform: translateY(-10px) rotate(0deg);
                opacity: 1;
            }

            100% {
                transform: translateY(100vh) rotate(720deg);
                opacity: 0;
            }
        }
    </style>
</head>

<body>
    <h1><i class="fas fa-gift"></i> 幸运抽奖转盘 <i class="fas fa-gift"></i></h1>

    <div class="container">
        <div class="panel control-panel">
            <h2>转盘设置</h2>

            <div class="input-group">
                <label for="item-count"><i class="fas fa-list-ol"></i> 商品数量 (2-12):</label>
                <input type="number" id="item-count" min="2" value="8">
            </div>

            <div class="input-group">
                <label><i class="fas fa-tags"></i> 商品名称:</label>
                <div class="item-inputs" id="item-inputs">
                    <!-- 动态生成输入框 -->
                </div>
                <button class="add-btn" id="add-item-btn">
                    <i class="fas fa-plus"></i> 增加奖项
                </button>
            </div>

            <div class="update-hint" id="update-hint">* 输入已实时更新到转盘</div>

            <button id="update-btn"><i class="fas fa-sync-alt"></i> 确认更新转盘</button>

            <div class="instructions">
                <h3><i class="fas fa-info-circle"></i> 使用说明</h3>
                <ul>
                    <li>输入商品数量（2-12之间）</li>
                    <li>自定义每个商品的名称，可添加或删除</li>
                    <li>输入会实时更新到转盘上</li>
                    <li>点击"开始抽奖"按钮旋转转盘</li>
                    <li>转盘停止后，指针所指区域即为中奖商品</li>
                </ul>
            </div>
        </div>

        <div class="panel">
            <h2>幸运抽奖</h2>

            <div class="wheel-container">
                <div class="pointer"></div>
                <div class="wheel" id="wheel">
                    <div class="wheel-inner" id="wheel-inner">
                        <div class="center-circle">GO!</div>
                    </div>
                </div>
            </div>

            <button class="spin-btn" id="spin-btn"><i class="fas fa-play"></i> 开始抽奖</button>

            <div class="result" id="result">
                <i class="fas fa-star"></i>
                <div>等待抽奖...</div>
            </div>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function () {
            const wheel = document.getElementById('wheel');
            const wheelInner = document.getElementById('wheel-inner');
            const itemCountInput = document.getElementById('item-count');
            const updateBtn = document.getElementById('update-btn');
            const spinBtn = document.getElementById('spin-btn');
            const resultDiv = document.getElementById('result');
            const itemInputsContainer = document.getElementById('item-inputs');
            const addItemBtn = document.getElementById('add-item-btn');
            const updateHint = document.getElementById('update-hint');
            const centerCircle = document.querySelector('.center-circle');

            // 初始颜色方案 - 更鲜艳和谐的配色
            const colors = [
                '#FF6B6B', '#FFD166', '#06D6A0', '#118AB2',
                '#073B4C', '#9381FF', '#F8B195', '#F67280',
                '#C06C84', '#6C5B7B', '#355C7D', '#2A9D8F'
            ];

            // 初始商品名称
            let userItems = [
            ];

            // 防抖函数
            function debounce(func, delay = 300) {
                let timeout;
                return function (...args) {
                    clearTimeout(timeout);
                    timeout = setTimeout(() => func.apply(this, args), delay);
                };
            }

            // 生成商品名称输入框
            function generateItemInputs() {
                itemInputsContainer.innerHTML = '';
                userItems.forEach((item, index) => {
                    const itemDiv = document.createElement('div');
                    itemDiv.className = 'item-input';

                    const input = document.createElement('input');
                    input.type = 'text';
                    input.className = 'item-name';
                    input.value = item;
                    input.dataset.index = index;

                    // 输入事件监听，实时更新转盘
                    input.addEventListener('input', debounce(function () {
                        userItems[index] = this.value || `奖项${index + 1}`;
                        initWheel();
                        updateHint.classList.add('show');
                        setTimeout(() => {
                            updateHint.classList.remove('show');
                        }, 3000);
                    }));

                    const deleteBtn = document.createElement('button');
                    deleteBtn.innerHTML = '<i class="fas fa-trash"></i>';
                    deleteBtn.onclick = () => removeItem(index);

                    itemDiv.appendChild(input);
                    itemDiv.appendChild(deleteBtn);
                    itemInputsContainer.appendChild(itemDiv);
                });

                itemCountInput.value = userItems.length;
            }

            // 添加新奖项
            function addItem() {
                userItems.push(`新奖项${userItems.length + 1}`);
                generateItemInputs();
                initWheel();
                updateHint.classList.add('show');
                setTimeout(() => {
                    updateHint.classList.remove('show');
                }, 3000);
            }


            // 删除奖项
            function removeItem(index) {
                userItems.splice(index, 1);
                generateItemInputs();
                initWheel();
                updateHint.classList.add('show');
                setTimeout(() => {
                    updateHint.classList.remove('show');
                }, 3000);
            }

            // 获取用户输入的商品名称
            function getUserItems() {
                const inputs = document.querySelectorAll('.item-name');
                return Array.from(inputs).map(input => input.value || `奖项${parseInt(input.dataset.index) + 1}`);
            }

            // 初始化转盘
            function initWheel() {
                const itemCount = getUserItems().length;
                const currentItems = getUserItems();

                // 清除现有内容
                wheelInner.innerHTML = '<div class="center-circle">GO!</div>';

                // 创建渐变色字符串
                let gradientString = 'conic-gradient(';
                const degreePerItem = 360 / itemCount;

                for (let i = 0; i < itemCount; i++) {
                    const startDegree = i * degreePerItem;
                    const endDegree = (i + 1) * degreePerItem;

                    gradientString += `${colors[i % colors.length]} ${startDegree}deg ${endDegree}deg`;

                    if (i < itemCount - 1) {
                        gradientString += ', ';
                    }
                }

                gradientString += ')';

                // 设置转盘背景
                wheelInner.style.background = gradientString;

                // 添加商品标签
                for (let i = 0; i < itemCount; i++) {
                    const label = document.createElement('div');
                    label.className = 'sector-label';
                    label.textContent = currentItems[i];

                    const angle = i * degreePerItem + degreePerItem / 2;
                    const reverseAngle = -angle;

                    label.style.setProperty('--angle', `${angle}deg`);
                    label.style.setProperty('--reverse-angle', `${reverseAngle}deg`);

                    wheelInner.appendChild(label);
                }
            }


            // 更新转盘
            updateBtn.addEventListener('click', function () {
                const itemCount = parseInt(itemCountInput.value);

                userItems = getUserItems();

                if (itemCount > userItems.length) {
                    for (let i = userItems.length; i < itemCount; i++) {
                        userItems.push(`奖项${i + 1}`);
                    }
                } else if (itemCount < userItems.length) {
                    userItems = userItems.slice(0, itemCount);
                }

                generateItemInputs();
                initWheel();
                resultDiv.innerHTML = '<i class="fas fa-star"></i><div>转盘已更新，点击"开始抽奖"</div>';
                updateHint.classList.remove('show');

                // 添加按钮点击反馈
                this.classList.add('clicked');
                setTimeout(() => this.classList.remove('clicked'), 300);
            });

            // 添加奖项
            addItemBtn.addEventListener('click', addItem);

            // 中心按钮也可以启动抽奖
            centerCircle.addEventListener('click', function () {
                if (!spinBtn.disabled) {
                    spinBtn.click();
                }
            });

            // 开始抽奖
            spinBtn.addEventListener('click', function () {
                const currentItems = getUserItems();
                const itemCount = currentItems.length;

                // 禁用按钮防止重复点击
                spinBtn.disabled = true;
                updateBtn.disabled = true;
                addItemBtn.disabled = true;
                centerCircle.style.cursor = 'not-allowed';

                // 计算旋转角度
                const degreePerItem = 360 / itemCount;
                const extraRotations = 5 + Math.round(Math.random() * 2); // 随机多转5-7圈
                const randomIndex = Math.floor(Math.random() * itemCount);
                const finalRotation = extraRotations * 360 + (360 - randomIndex * degreePerItem - degreePerItem / 2);

                // 设置旋转动画
                wheel.style.setProperty('--rotation', `${finalRotation}deg`);
                wheel.classList.add('spin-animation');

                // 更新结果区域
                resultDiv.innerHTML = '<i class="fas fa-spinner fa-spin"></i><div>转盘旋转中...</div>';

                // 显示结果
                setTimeout(function () {
                    const prize = currentItems[randomIndex];
                    resultDiv.innerHTML = `<i class="fas fa-trophy"></i><div>恭喜您获得：${prize}！</div>`;

                    spinBtn.disabled = false;
                    updateBtn.disabled = false;
                    addItemBtn.disabled = false;
                    centerCircle.style.cursor = 'pointer';
                }, 4000);

                // 重置动画
                setTimeout(function () {
                    wheel.classList.remove('spin-animation');
                    wheel.style.transform = `rotate(${finalRotation}deg)`;
                }, 5000);
            });

            // 监听数量输入框变化
            itemCountInput.addEventListener('change', function () {
                let count = parseInt(this.value);
    

                if (count > userItems.length) {
                    for (let i = userItems.length; i < count; i++) {
                        userItems.push(`奖项${i + 1}`);
                    }
                } else if (count < userItems.length) {
                    userItems = userItems.slice(0, count);
                }
                generateItemInputs();
                initWheel();
                updateHint.classList.add('show');
                setTimeout(() => {
                    updateHint.classList.remove('show');
                }, 3000);
            });

            // 初始化
            generateItemInputs();
            initWheel();
        });
    </script>
</body>

</html>